package com.shouke.jmeter.plugin.sampler;

import org.apache.jmeter.testbeans.BeanInfoSupport;
import org.apache.jmeter.testbeans.gui.TypeEditor;
import org.apache.jmeter.testbeans.gui.TableEditor;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;

import com.shouke.jmeter.plugin.model.FieldExpressionMapping;

public class RabbitMQPubSamplerBeanInfo extends BeanInfoSupport {
    private static final String[] BOOLEAN_OPTIONS = new String[2];
    private static final String[] DATA_TYPE_OPTIONS = new String[2];

    /**
     * 设置列表选项
     */
    static {
        BOOLEAN_OPTIONS[0] = "true";
        BOOLEAN_OPTIONS[1] = "false";

        DATA_TYPE_OPTIONS[0] = "JSON";
        DATA_TYPE_OPTIONS[1] = "String";
    }

    public RabbitMQPubSamplerBeanInfo(){
        super(RabbitMQPubSampler.class);

        createPropertyGroup("Variable Name bound to rabbitMQ connection", new String[] {"rabbitMQConnVariableName"});
        PropertyDescriptor rabbitMQConnVariableNamePropDesc =  property("rabbitMQConnVariableName");
        rabbitMQConnVariableNamePropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        rabbitMQConnVariableNamePropDesc.setValue(DEFAULT, "rabbitMQConn");
        rabbitMQConnVariableNamePropDesc.setDisplayName("Connection variable name");
        rabbitMQConnVariableNamePropDesc.setShortDescription("Variable name declared in RabbitMQ Config Element");

        // 创建队列属性组，实现配置元件界面“组框”的效果
        createPropertyGroup("Publish Message Configs", new String[] {
                "exchangeName",
                "queueName",
                "isCreateQueue",
                "routingKey",
                "messageHeaders",
                "messageProperties",
                "dataType",
                "message"
        });

        // 在Jmeter GUI 组框中添加字段及设置
        PropertyDescriptor PubMsgConfigPropDesc =  property("exchangeName");
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, "");
        PubMsgConfigPropDesc.setDisplayName("Exchange Name");
        PubMsgConfigPropDesc.setShortDescription("name of exchange name");
        
        PubMsgConfigPropDesc =  property("queueName");
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, "");
        PubMsgConfigPropDesc.setDisplayName("Queue Name");
        PubMsgConfigPropDesc.setShortDescription("name of rabbitmq queue");

        PubMsgConfigPropDesc =  property("isCreateQueue", TypeEditor.ComboStringEditor);
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, BOOLEAN_OPTIONS[1]);
        PubMsgConfigPropDesc.setValue(NOT_OTHER, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(TAGS, BOOLEAN_OPTIONS);
        PubMsgConfigPropDesc.setDisplayName("If Create Queue");
        PubMsgConfigPropDesc.setShortDescription("if queue with Quueue Name not extists, and neee to create queue");

        PubMsgConfigPropDesc =  property("routingKey");
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, "");
        PubMsgConfigPropDesc.setDisplayName("Routing Key");
        PubMsgConfigPropDesc.setShortDescription("routing key for binding exchange");

        PubMsgConfigPropDesc = property("messageHeaders", TypeEditor.TableEditor);
        PubMsgConfigPropDesc.setValue(TableEditor.CLASSNAME, FieldExpressionMapping.class.getName());
        PubMsgConfigPropDesc.setValue(TableEditor.HEADERS, new String[]{ "MsgHeader", "Value" } );
        PubMsgConfigPropDesc.setValue(TableEditor.OBJECT_PROPERTIES, new String[]{ FieldExpressionMapping.MSG_HEAD, FieldExpressionMapping.MSG_HEAD_VALUE} );
        PubMsgConfigPropDesc.setValue(DEFAULT, new ArrayList<>());
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);

        PubMsgConfigPropDesc= property("messageProperties", TypeEditor.TableEditor);
        PubMsgConfigPropDesc.setValue(TableEditor.CLASSNAME, FieldExpressionMapping.class.getName());
        PubMsgConfigPropDesc.setValue(TableEditor.HEADERS, new String[]{ "MsgProperty", "Value" } );
        PubMsgConfigPropDesc.setValue(TableEditor.OBJECT_PROPERTIES, new String[]{ FieldExpressionMapping.MSG_PROPERTY, FieldExpressionMapping.MSG_PROPERTY_VALUE} );
        PubMsgConfigPropDesc.setValue(DEFAULT, new ArrayList<>());
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);

        PubMsgConfigPropDesc =  property("dataType", TypeEditor.ComboStringEditor);
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, DATA_TYPE_OPTIONS[0]);
        PubMsgConfigPropDesc.setValue(NOT_OTHER, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(TAGS, DATA_TYPE_OPTIONS);
        PubMsgConfigPropDesc.setDisplayName("Message Data Type");
        PubMsgConfigPropDesc.setShortDescription("Message data type");

        PubMsgConfigPropDesc =  property("message", TypeEditor.TextAreaEditor);
        PubMsgConfigPropDesc.setValue(NOT_UNDEFINED, Boolean.TRUE);
        PubMsgConfigPropDesc.setValue(DEFAULT, "");
        PubMsgConfigPropDesc.setDisplayName("Message");
        PubMsgConfigPropDesc.setShortDescription("message");

    }
}